package com.cug.campus_sh_market_server.controller;

import com.cug.campus_sh_market_server.config.aop.ValidateAspect;
import com.cug.campus_sh_market_server.entity.param.OrderParam;
import com.cug.campus_sh_market_server.entity.param.PageParam;
import com.cug.campus_sh_market_server.entity.po.BaseEntity;
import com.cug.campus_sh_market_server.entity.vo.ResultData;
import com.cug.campus_sh_market_server.service.IRestfullService;
import com.nycreativity.elecreativity.entity.vo.PageDataVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author sxt
 * @version 1.0
 * @description TODO
 * @date 2022/3/29 22:56
 */
public abstract class AbsRestfullController
        <Entity extends BaseEntity, GetParam extends BaseEntity, AddParam extends BaseEntity, PutParam extends BaseEntity, VO extends BaseEntity> {

//    public static @interface

    @Autowired
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    protected IRestfullService<Entity, GetParam, AddParam, PutParam, VO> restfullService;

    @PostMapping
    @ValidateAspect.ParamValidate
    @ApiOperation("添加数据")
    protected ResultData<VO> addData(@RequestBody @Valid AddParam addParam, BindingResult result) {
        VO save = restfullService.saveWithBackData(addParam);
        return save != null ? ResultData.SUCCESS("数据添加成功", save) : ResultData.FAILED("数据添加失败", null);
    }

    @DeleteMapping
    @ApiOperation("删除数据")
    protected ResultData<Boolean> removeData(Integer id) {
        boolean removeData = restfullService.removeData(id);
        return removeData ? ResultData.SUCCESS("数据删除成功", true) : ResultData.FAILED("数据删除失败", false);
    }

    @PutMapping
    @ValidateAspect.ParamValidate
    @ApiOperation("修改数据")
    protected ResultData<Boolean> putData(@RequestBody @Valid PutParam putParam, BindingResult result) {
        boolean updateData = restfullService.updateData(putParam);
        return updateData ? ResultData.SUCCESS("数据修改成功", true) : ResultData.FAILED("数据修改失败", false);
    }

    @GetMapping
    @ApiOperation("查询数据")
    protected ResultData<List<VO>> getData(GetParam getParam, OrderParam orderParam) {
        List<VO> queryData = restfullService.queryData(getParam, orderParam);
        return ResultData.SUCCESS("成功", queryData);
    }

    @GetMapping("/page")
    @ApiOperation("查询数据(带分页)")
    protected ResultData<PageDataVO<VO>> getDataByPage(GetParam getParam, PageParam pageParam, OrderParam orderParam) {
        PageDataVO<VO> pageDataVO = restfullService.queryDataByPage(getParam, new PageParam(pageParam.getCurrentPage(), pageParam.getPageSize()), orderParam);
        return ResultData.SUCCESS("成功", pageDataVO);
    }

}
